package cn.edu.cjw.APMFP.user.DAO;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.ResourceBundle;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import cn.edu.cjw.APMFP.RoleRight.Service.RoleRightService;
import cn.edu.cjw.APMFP.RoleRight.Service.RrServiceImpl;
import cn.edu.cjw.APMFP.Util.HBaseUtill;
import cn.edu.cjw.APMFP.Util.HiveUtill;
import cn.edu.cjw.APMFP.user.pojo.User;

public class UserDAOImpl implements UserDAO {

	RoleRightService rrs = new RrServiceImpl();

	public boolean AddAndUpdateUser(User user) throws IOException {

		// 建立HBase连接，获取Connection对象
		Connection conn = HBaseUtill.getConn();

		// 选取操作对象
		Table table = conn.getTable(TableName.valueOf(user.USER_TABLE_NAME));

		// 对数据进行处理
		Put put = HBaseUtill.putUser(user);
		table.put(put);

		// 释放资源
		HBaseUtill.Close(table, conn);

		return true;
	}

	public boolean RemoveUser(User user) throws IOException {

		// 建立HBase连接，获取Connection对象
		Connection conn = HBaseUtill.getConn();

		// 选取操作对象
		Table table = conn.getTable(TableName.valueOf(user.USER_TABLE_NAME));

		// 对数据进行处理
		Delete delete = new Delete(Bytes.toBytes(user.getAccount()));
		table.delete(delete);

		// 释放资源
		HBaseUtill.Close(table, conn);

		return true;
	}

	public User SearchUserByAccount(String account) throws Exception {

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String HiveUrl = bundle.getString("Hive-url");
		String HiveUser = bundle.getString("Hive-user");
		String HivePassword = bundle.getString("Hive-password");

		User user = new User();

		// 调用HiveUtill工具，获取Hive的Connection连接
		java.sql.Connection conn = HiveUtill.getConn(HiveUrl, HiveUser, HivePassword);

		// 选取将要操作的数据库
		String sql = "SELECT * FROM apmfp_user_table WHERE account=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, account);

		// 执行SQL
		ResultSet rs = ps.executeQuery();

		// 处理数据
		while (rs.next()) {

			user = new User(rs.getString("account"), rs.getString("password"), rs.getString("name"),
					rrs.searchRoleByNum(rs.getString("uRole")), rs.getString("gender"), rs.getInt("age"),
					Boolean.valueOf(rs.getString("Deactivate")));

		}

		// 释放资源
		HiveUtill.Close(rs, ps, conn);

		return user;
	}

	public ArrayList<User> SearchUsersByRole(String rnum) throws Exception {

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String HiveUrl = bundle.getString("Hive-url");
		String HiveUser = bundle.getString("Hive-user");
		String HivePassword = bundle.getString("Hive-password");

		ArrayList<User> list = new ArrayList<User>();

		// 调用HiveUtill工具，获取Hive的Connection连接
		java.sql.Connection conn = HiveUtill.getConn(HiveUrl, HiveUser, HivePassword);

		// 选取将要操作的数据库
		String sql = "SELECT * FROM apmfp_user_table WHERE urole=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, rnum);

		// 执行SQL
		ResultSet rs = ps.executeQuery();

		// 处理数据
		while (rs.next()) {

			User user = new User(rs.getString("account"), rs.getString("password"), rs.getString("name"),
					rrs.searchRoleByNum(rs.getString("uRole")), rs.getString("gender"), rs.getInt("age"),
					Boolean.valueOf(rs.getString("Deactivate")));

			list.add(user);
		}

		// 释放资源
		HiveUtill.Close(rs, ps, conn);

		return list;
	}

	public ArrayList<User> SearchUserByName(String name) throws Exception {

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String HiveUrl = bundle.getString("Hive-url");
		String HiveUser = bundle.getString("Hive-user");
		String HivePassword = bundle.getString("Hive-password");

		ArrayList<User> list = new ArrayList<User>();

		// 调用HiveUtill工具，获取Hive的Connection连接
		java.sql.Connection conn = HiveUtill.getConn(HiveUrl, HiveUser, HivePassword);

		// 选取将要操作的数据库
		String sql = "SELECT * FROM apmfp_user_table WHERE name=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, name);

		// 执行SQL
		ResultSet rs = ps.executeQuery();

		// 处理数据
		while (rs.next()) {

			User user = new User(rs.getString("account"), rs.getString("password"), rs.getString("name"),
					rrs.searchRoleByNum(rs.getString("uRole")), rs.getString("gender"), rs.getInt("age"),
					Boolean.valueOf(rs.getString("Deactivate")));

			list.add(user);
		}

		// 释放资源
		HiveUtill.Close(rs, ps, conn);

		return list;
	}

	public ArrayList<User> showAll() throws Exception {

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String HiveUrl = bundle.getString("Hive-url");
		String HiveUser = bundle.getString("Hive-user");
		String HivePassword = bundle.getString("Hive-password");

		ArrayList<User> list = new ArrayList<User>();

		// 调用HiveUtill工具，获取Hive的Connection连接
		java.sql.Connection conn = HiveUtill.getConn(HiveUrl, HiveUser, HivePassword);

		// 选取将要操作的数据库
		String sql = "SELECT * FROM apmfp_user_table ";
		PreparedStatement ps = conn.prepareStatement(sql);

		// 执行SQL
		ResultSet rs = ps.executeQuery();

		// 处理数据
		while (rs.next()) {

			User user = new User(rs.getString("account"), rs.getString("password"), rs.getString("name"),
					rrs.searchRoleByNum(rs.getString("uRole")), rs.getString("gender"), rs.getInt("age"),
					Boolean.valueOf(rs.getString("Deactivate")));

			list.add(user);
		}

		// 释放资源
		HiveUtill.Close(rs, ps, conn);

		return list;
	}

}
